local function upgrade_hammer_type1(inst)
    inst.CHAT_TABLE = {}
    pcall(function()
        inst.CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["hammer_upgrade_mole"] or {}
    end)

    
    inst.talk_sound = "moonstorm/characters/wagstaff/talk_single"
    inst:RemoveTag("shadowlevel")
    if inst.components.named.name == nil then
        inst.components.named.possiblenames = inst.npc_base_lib:Get_Names()
        inst.components.named:PickNewName()
    end

    local function Chat_Bubble_Fn(inst)
        inst.npc_base_lib:Show_Chat_Bubble({
            monster = "mole",
            -- monster = boss_name,
            height = 2
        })
        inst.npc_base_lib:Say(inst.CHAT_TABLE.ask_for_target,true)
    end


    inst:ListenForEvent("ondropped",function()
        -- print("bat bat drop event")
        inst.__Chat_Bubble_Drop_Task =inst:DoTaskInTime(3,function()
            if inst.components.inventoryitem.owner == nil then
                Chat_Bubble_Fn(inst)
                inst:DoTaskInTime(5,function()
                    inst.npc_base_lib:Hide_Chat_Bubble()
                end)
            end
        end)
    end)

    inst:ListenForEvent("onpickup",function()
        if inst.__Chat_Bubble_Drop_Task then
            inst.__Chat_Bubble_Drop_Task:Cancel()
            inst.__Chat_Bubble_Drop_Task = nil
        end
        inst.npc_base_lib:Hide_Chat_Bubble()
        -- print("batbat pickup event")
    end)

    inst:DoPeriodicTask(10,function()
        if inst.components.inventoryitem.owner ~= nil then
            return
        end

        if inst:IsNearPlayer(20) == true then
            Chat_Bubble_Fn(inst)
            inst:DoTaskInTime(5,function()
                inst.npc_base_lib:Hide_Chat_Bubble()
            end)
        end

    end)

    ------------------------------------------------------------------------------------------------------------------------------------

    local function mole_smart_task(inst)
        local x,y,z = inst.Transform:GetWorldPosition()
        local ents = TheSim:FindEntities(x, 0, z, 30, {"mole"} )
        for k, v in pairs(ents) do
            if v and v:IsValid() then
                if v.sg:HasStateTag("sleep") or v.sg:HasStateTag("sleeping") then
                    v.sg:GoToState("peek")
                end
                v:RestartBrain()
            end
        end
    end

    local function worng_target_say(inst)
        if inst.__worng_target_say_flag_task ~= nil then
            return
        end
        inst.__worng_target_say_flag_task = inst:DoTaskInTime(4,function()
            inst.__worng_target_say_flag_task = nil
        end)

        inst.npc_base_lib:Say(inst.CHAT_TABLE.worng_target,true)
    end

    local function Spawn_mole_in_random(inst,immediately_flag)
        if immediately_flag ~= true then
            if inst.__spawn_mole_cooldown_task ~= nil then
                return
            end
            inst.__spawn_mole_cooldown_task = inst:DoTaskInTime(5,function()
                inst.__spawn_mole_cooldown_task = nil
            end)
        end

        local pt = Vector3( inst.Transform:GetWorldPosition() )
        local Spawn_pt = inst.npc_base_lib:GetSpawnPoint(pt,math.random(3,10)) or pt
        local monster = SpawnPrefab("mole")
        monster.Transform:SetPosition(Spawn_pt.x,0,Spawn_pt.z)
        monster.sg:GoToState("peek")
        monster:RestartBrain()
    end  

    local function right_target_fn(inst,target)
        local gold_num = 1
        local finiteuses_use_base = inst.components.finiteuses.total / 300
        local finiteuses_use = 1
        if TUNING.NPC_CONFIG.REWARDS_LESS then
            if math.random(1000)/10 <= 5 then
                gold_num = math.random(5,15)
                inst.SoundEmitter:PlaySound("dontstarve_DLC001/common/glommer_bell")
            else
                gold_num = 1
                inst.SoundEmitter:PlaySound("dontstarve_DLC001/common/firesupressor_warningbell")         
            end
            finiteuses_use = finiteuses_use_base * 16
        else    ------------- more reward
            if math.random(1000)/10 <= 5 then
                gold_num = math.random(10,30)
                inst.SoundEmitter:PlaySound("dontstarve_DLC001/common/glommer_bell")         

            else
                gold_num = math.random(3)
                inst.SoundEmitter:PlaySound("dontstarve_DLC001/common/firesupressor_warningbell")         
            end
            finiteuses_use = finiteuses_use_base * 6
        end

        inst.npc_base_lib:Throw_Out_Items({
            target = target,
            name = "goldnugget",
            num = gold_num,
            range = math.random(3,8),
            height = 8
        })
        inst.components.finiteuses:Use(finiteuses_use,true)
        target:Remove()
        Spawn_mole_in_random(inst,true)
    end

    inst.components.weapon.onattack = function(inst, owner, target) 
        if owner == nil or not owner:HasTag("player") then
            return
        end
        if target == nil then
            return
        end

        if target.prefab == "mole" then
            right_target_fn(inst,target)
        else
            worng_target_say(inst)
            mole_smart_task(inst)
            Spawn_mole_in_random(inst)
        end
    end

    ------ 不能用 tool 组件，会导致 stack 崩溃
    -- inst.components.tool.GetEffectiveness__npc_old = inst.components.tool.GetEffectiveness
    -- inst.components.tool.GetEffectiveness = function(self,...)
    --     if self._____worngtarget_flag ~= true then
    --         self._____worngtarget_flag = true
    --         worng_target_say(self.inst)
    --         Spawn_mole_in_random(self.inst)
    --         self.inst:DoTaskInTime(5,function()
    --             self._____worngtarget_flag = false
    --         end)
    --     end
    --     return self.GetEffectiveness__npc_old(self,...)
    -- end
    ------------------------------------------------------------------------------------------------------------------------------------
    inst.components.finiteuses:SetConsumption(ACTIONS.HAMMER, 0)    --- 设置锤建筑时候使用的耐久度
    ------------------------------------------------------------------------------------------------------------------------------------
    ----- 耐久度修改
    inst.components.finiteuses.Use = function(self,num,flag)
        if flag == true then
            self:SetUses(self.current - (num or 1))
        end
        ---------  锤建筑判断
        if num == 0 and flag == nil and self._____worngtarget_flag ~= true then
            self._____worngtarget_flag = true
            worng_target_say(self.inst)
            Spawn_mole_in_random(self.inst)
            self.inst:DoTaskInTime(5,function()
                self._____worngtarget_flag = false
            end)
        end

    end
    ------------------------------------------------------------------------------------------------------------------------------------
end

AddPrefabPostInit(
    "hammer",
    function(inst)
        
        if inst.SoundEmitter == nil then
            inst.entity:AddSoundEmitter()
        end

        ---------------------------------------------------------------------------------------------
        -- talker 组件   net_bool 帮助处理偏移
        -- local TALLER_TALKER_OFFSET = Vector3(0, -700, 0)
        local colours = {
            Vector3( 255 /255, 193 /255 , 37 /255),     -- Goldenrod1 橙黄色
            Vector3( 127 /255, 255 /255 , 0 /255),      --- Chartreuse 青绿色
            Vector3( 15 /255, 15 /255 , 15 /255),      --- black 黑色
            Vector3( 255 /255, 69 /255 , 0 /255),       -- OrangeRed 橘红色
            Vector3( 0 /255, 191 /255 , 255 /255),      -- DeepSkyBlue 天蓝色
            Vector3( 34 /255, 139 /255 , 34 /255),      -- ForestGreen 深绿色
            Vector3( 238 /255, 233 /255 , 233 /255),     -- Snow2  雪白色
        }
        local DEFAULT_TALKER_OFFSET = Vector3(0, 0, 0)
        inst:AddComponent("talker")
        inst.components.talker.offset = DEFAULT_TALKER_OFFSET
        inst.components.talker.fontsize = 28
        inst.components.talker.font = TALKINGFONT
        inst.components.talker.color_num = 1
        inst.components.talker.colour = colours[inst.components.talker.color_num]
        inst.components.talker.offset = Vector3(0, 0, 0)
        inst.components.talker.Say_old_batbat = inst.components.talker.Say
        inst.components.talker.Say = function(self,...)
            self:Say_old_batbat(...)
            self.color_num = self.color_num + 1
            if self.color_num > #colours then
                self.color_num = 1
            end
            self.colour = colours[self.color_num]
        end
        ---------------------------------------------------------------------------------------------

        if not TheWorld.ismastersim then ---------------------------------------------------------------------------------------------
            return
        end

        -- inst:AddComponent("tradable")
        inst:AddComponent("npc_base_lib")
        inst:AddComponent("npc_everything_data")

        if inst.components.named == nil then
            inst:AddComponent("named")
        end

        
        ------------------------------------------------------------------------------------------------
        inst:ListenForEvent("upgrade",function(inst,str)
            if str == nil then
                return
            end
            if str == "gopher" then
                upgrade_hammer_type1(inst)
                inst.components.npc_everything_data:Set("upgrade","gopher")
            end
        end)

        inst:DoTaskInTime(0,function()
            local the_type =  inst.components.npc_everything_data:Get("upgrade")
            if the_type == "gopher" then
                upgrade_hammer_type1(inst)
            end
        end)
        ------------------------------------------------------------------------------------------------






        ------------------------------------------------------------------------------------------------
        
    end
)